<?php

namespace C\S\User;

use C\L\Service;

class Funds extends Service
{
    protected function setModel()
    {
        $this->model = new \C\M\UserFunds();
    }

    public function add(
        $uid,
        $money,
        $type,
        $stype,
        $title,
        $cid,
        $status = 'D',
        $beforMoney = 0,
        $afterMoney = 0,
        $remark = ''
    ) {
        $add = [
        'cid' => $cid,
        'uid' => $uid,
        'money' => $money,
        'type' => $type,
        'stype' => $stype,
        'title' => $title,
        'status' => $status,
        'befor_money' => $beforMoney,
        'after_money' => $afterMoney,
        'btype' => $money < 0 ? 'sub' : 'add',
        'remark' => $remark
        ];

        return $this->save($add);
    }

    public function getStatusConfig()
    {
        return [
        'status' => [
        'D' => '待处理',
        'S' => '处理中',
        'Y' => '处理成功'
        ],
        'type' => [
        'money'  => '金额',
        'credit' => '成长值',
        ],
        'stype' => [
        'confirm_group_buying_order'      => '发起团购',
        'confirm_group_buying_user_order' => '参与团购',
        'item_apr_money'                  => '返佣收益',
        'item_back_money'                 => '本金返还',
        'invest_wx'                       => '微信充值',
        'invest_alipay'                   => '支付宝充值',
        'invest_bank'                     => '银行卡充值',
        'withdraw'                        => '提现',
        'checkin'                         => '签到',
        'new_user_bonus'                  => '新人福利',
        'item_raward'                     => '推广奖励',
        'image_share'                     => '分享奖励',
        ],
        ];
    }

    public function getTabs()
    {
        return [
        'confirm_group_buying_order' => [
        'text'   => '发起团购',
        'stypes' => [
          'confirm_group_buying_order',
        ],
        ],
        'confirm_group_buying_user_order' => [
        'text'   => '参与团购',
        'stypes' => [
          'confirm_group_buying_user_order',
        ],
        ],
        'profit' => [
        'text'   => '返佣收益',
        'stypes' => [
          'item_apr_money',
        ],
        ],
        'return_money' => [
        'text'   => '本金返还',
        'stypes' => [
          'item_back_money',
        ],
        ],
        'recharge_wx' => [
        'text'   => '微信充值',
        'stypes' => [
          'invest_wx',
        ],
        ],
        'recharge_alipay' => [
        'text'   => '支付宝充值',
        'stypes' => [
          'invest_alipay',
        ],
        ],
        'recharge_bank' => [
        'text'   => '银行卡充值',
        'stypes' => [
          'invest_bank',
        ],
        ],
        'withdraw' => [
        'text'   => '提现',
        'stypes' => [
          'withdraw',
        ],
        ],
        'checkin_reward' => [
        'text'   => '签到',
        'stypes' => [
          'checkin',
        ],
        ],
        'invite_raward' => [
        'text'   => '推广奖励',
        'stypes' => [
          'group_buying_confirm_order_superior_reward',
          'group_buying_confirm_user_order_superior_reward',
        ],
        ],
        'share_reward' => [
        'text'   => '分享奖励',
        'stypes' => [
          'image_share',
        ],
        ],
        ];
    }

    public function applyPack($name, $vip, $money)
    {
        try {
            if (empty($name)) {
                throw new \Exception('红包名不能为空');
            }

            if ($money < 0.01) {
                throw new \Exception('红包金额最小为0.01');
            }

            $vipConfig = $this->di['s_level']->search($vip);
            if (empty($vipConfig)) {
                throw new \Exception('未找到当前vip配置');
            }

            $data = [];
            $scores = $this->di['s_level']->getNextLevelScore($vip);
            if ($scores[1] > 0) {
                $data['data']['credit'] = $scores;
                $data['data_type']['credit'] = 'between';
            } else {
                $data['data']['credit'] = $scores[0];
                $data['data_type']['credit'] = '>=';
            }

            if (empty($data)) {
                throw new \Exception('添加失败');
            }

            $users = $this->di['s_user']->searchAll($data['data'], $data['data_type'], ['uid', 'credit']);
            if (empty($users)) {
                throw new \Exception('当前VIP等级下无用户');
            }

            $adds = [];
            $time = time();
            foreach ($users as $user) {
                $adds[] = [
                'cid' => $user['uid'],
                'uid' => $user['uid'],
                'money' => $money,
                'type' => 'money',
                'stype' => 'pack_send',
                'title' => $name,
                'status' => 'D',
                'uptime' => $time,
                'addtime' => $time,
                ];
            }

            if (!$this->saves($adds)) {
                throw new \Exception('添加失败');
            }

            return true;
        } catch (\Exception $e) {
            $this->di['message']->setSerMsg($e->getMessage());
            return false;
        }
    }
}
